package com.yitong.base.dao.impl;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Repository;

import com.google.gson.reflect.TypeToken;
import com.yitong.base.dao.api.Login;
import com.yitong.base.domain.Message;
import com.yitong.base.util.Constant;
import com.yitong.base.util.HttpUtil;
import com.yitong.base.util.JsonUtils;
import com.yitong.profile.domain.TabRight;
import com.yitong.profile.domain.TabUser;

@Repository(value="login")
public class LoginImpl implements Login{
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Message jsonStrToMessageMap(String jsonStr){
		Message message=null;
	   // jsonStr=jsonStr.replace("\\", "");
		Map<String, Object> map=JsonUtils.parseJSON2Map(jsonStr);
//		Type type=new TypeToken<Message<TabUser>>(){}.getType();
//		message=(Message<TabUser>)JsonUtils.jsonToBean(jsonStr, type);
		if(map!=null){
			if(map!=null){
				message=new Message();
				message.setCode(Integer.parseInt(map.get(Constant.MESSAGE_CODE)+""));
				message.setInfo(map.get(Constant.MESSAGE_INFO)==null?"":map.get(Constant.MESSAGE_INFO));
				message.setMessage(map.get(Constant.MESSAGE_MESSAGE)+"");
			}
		}
		if(message==null){
			message=new Message();
			message.setCode(0);
			message.setMessage("json转化错误");
		}
		return message;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Message jsonStrToMessageTabUser(String jsonStr){
		Message message=null;
		Type type=new TypeToken<Message<TabUser>>(){}.getType();
		message=(Message<TabUser>)JsonUtils.jsonToBean(jsonStr, type);
		if(message==null){
			message=new Message();
			message.setCode(0);
			message.setMessage("json转化错误");
		}
		return message;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Message jsonStrToMessageTabRight(String jsonStr){
		Message message=null;
		Type type=new TypeToken<Message<List<TabRight>>>(){}.getType();
		message=(Message<List<TabRight>>)JsonUtils.jsonToBean(jsonStr, type);
		if(message==null){
			message=new Message();
			message.setCode(0);
			message.setMessage("json转化错误");
		}
		return message;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Message jsonStrToMessageList(String jsonStr){
		Message message=null;
//		Map<String, Object> map=JsonUtils.parseJSON2ObjectList(jsonStr);
		Type type=new TypeToken<Message<List<String>>>(){}.getType();
		message=(Message<List<String>>)JsonUtils.jsonToBean(jsonStr, type);
//		if(map!=null){
//			if(map!=null){
//				message=new Message();
//				message.setCode(Integer.parseInt(map.get(Constant.MESSAGE_CODE)+""));
//				message.setInfo(map.get(Constant.MESSAGE_INFO)==null?"":map.get(Constant.MESSAGE_INFO));
//				message.setMessage(map.get(Constant.MESSAGE_MESSAGE)+"");
//			}
//		}
		if(message==null){
			message=new Message();
			message.setCode(0);
			message.setMessage("json转化错误");
		}
		return message;
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<String> getAllUserListByOrgId(int orgId, HttpServletRequest request) throws IOException{
		List<String> userList=null;
		String userListUrl=Constant.AUTH_URL+Constant.USER_LIST_BY_ORGID_ACTION+"orgId="+orgId;
		String userListContent=HttpUtil.doGet(userListUrl, request);
		Message message=jsonStrToMessageList(userListContent);
		if(message!=null){
			userList=(List<String>) message.getInfo();
		}
		return userList;
	}
	/**
	 * 获得当前登录用户下的right下的所有权限
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<TabRight> getRightSub(int userId, int rightId, HttpServletRequest request)
			throws IOException {
		List<TabRight> righSubtList = null;
		String authSubUrl = Constant.AUTH_URL + Constant.RIGHT_SUB_ACTION
				+ "rightId=" + rightId + Constant.URL_SPLIT + "userId="
				+ userId;
		String authSubContent = HttpUtil.doGet(authSubUrl, request);
		if (authSubContent != null) {
			Message userAuthSubMessage = jsonStrToMessageTabRight(authSubContent);
			if (userAuthSubMessage.getCode() != 0) {
				righSubtList = (List<TabRight>) userAuthSubMessage.getInfo();
			}
		}
		return righSubtList;
	}

	@Override
	public List<String> getAllUserListByOrgId(int orgId) throws IOException {
		// TODO Auto-generated method stub
		return null;
	}
	
}	
